﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Text;

/// <summary>
///Tools 的摘要说明
/// </summary>
public class Tools
{
    public Tools()
    {

    }
    /// <summary>
    /// 取得HTML中所有图片的 URL。
    /// </summary>
    /// <param name="sHtmlText">HTML代码</param>
    /// <returns>图片的URL列表</returns>
    public static string[] GetHtmlImageUrlList(string sHtmlText)
    {
        // 定义正则表达式用来匹配 img 标签
        Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);

        // 搜索匹配的字符串
        MatchCollection matches = regImg.Matches(sHtmlText);

        int i = 0;
        string[] sUrlList = new string[matches.Count];

        // 取得匹配项列表
        foreach (Match match in matches)
        {
            sUrlList[i++] = match.Groups["imgUrl"].Value;
        }
        return sUrlList;
    }
    public static string[] GetHtmlImageUrlList_img(string sHtmlText)
    {
        // 定义正则表达式用来匹配 img 标签
        Regex regImg = new Regex(@"(?is)<img[^<>]*src=""[^<>""]*?[^>]*>", RegexOptions.IgnoreCase);

        // 搜索匹配的字符串
        MatchCollection matches = regImg.Matches(sHtmlText);

        int i = 0;
        string[] sUrlList = new string[matches.Count];

        // 取得匹配项列表
        foreach (Match match in matches)
        {
            sUrlList[i++] = match.Groups[0].Value;

        }
        return sUrlList;
    }

    public static int[] GetHtmlImageUrlList_num(string sHtmlText)
    {
        // 定义正则表达式用来匹配 img 标签
        Regex regImg = new Regex(@"(?is)<img[^<>]*src=""[^<>""]*?[^>]*>", RegexOptions.IgnoreCase);

        // 搜索匹配的字符串
        MatchCollection matches = regImg.Matches(sHtmlText);

        int i = 0;
        int[] sUrlList = new int[matches.Count];

        // 取得匹配项列表
        foreach (Match match in matches)
        {
            sUrlList[i++] = match.Groups[0].Index;

        }
        return sUrlList;
    }


    public static string NoHTML(string Htmlstring)  //替换HTML标记
    {
        //删除脚本
        Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
        //删除HTML
        Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"<img[^>]*>;", "", RegexOptions.IgnoreCase);
        Htmlstring.Replace("<", "");
        Htmlstring.Replace(">", "");
        Htmlstring.Replace("\r\n", "");
        Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
        return Htmlstring;
    }


    /// <summary>
    /// 检查是否是图片文件
    /// </summary>
    /// <param name="upfile"></param>
    /// <returns></returns>
    public static bool IsAllowedExtension(FileUpload upfile)
    {
        string strOldFilePath = "";
        string strExtension = "";
        string[] arrExtension = { ".gif", ".jpg", ".bmp", ".png" };
        if (upfile.PostedFile.FileName != string.Empty)
        {
            strOldFilePath = upfile.PostedFile.FileName.ToLower();//获得文件的完整路径名 
            strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));//获得文件的扩展名，如：.jpg 
            for (int i = 0; i < arrExtension.Length; i++)
            {
                if (strExtension.Equals(arrExtension[i]))
                {
                    return true;
                }
            }
        }
        return false;
    }


    /// <summary>
    /// 检查是否是课件文件
    /// </summary>
    /// <param name="upfile"></param>
    /// <returns></returns>
    public static bool IsAllowedExtension_course(FileUpload upfile)
    {
        string strOldFilePath = "";
        string strExtension = "";
        string[] arrExtension = { ".ppt", ".pptx", ".doc", ".docx", ".rar", ".zip" };
        if (upfile.PostedFile.FileName != string.Empty)
        {
            strOldFilePath = upfile.PostedFile.FileName.ToLower();//获得文件的完整路径名 
            strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));//获得文件的扩展名，如：.jpg 
            for (int i = 0; i < arrExtension.Length; i++)
            {
                if (strExtension.Equals(arrExtension[i]))
                {
                    return true;
                }
            }
        }
        return false;
    }


    /// <summary>
    /// MD5加密
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string mymd5(string str)
    {
        string EnPswdStr = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        return (EnPswdStr);
    }

    /// <summary>
    /// 字符串过滤，长度检查
    /// </summary>
    /// <param name="inputString"></param>
    /// <param name="maxLength"></param>
    /// <returns></returns>
    public static string Clearstr(string inputString, int maxLength)  //字符串过滤
    {
        StringBuilder retVal = new StringBuilder();
        if ((inputString != null) && (inputString != String.Empty))  //检查是否为空
        {
            inputString = inputString.Trim();
            if (inputString.Length > maxLength)  //检查长度
            {
                inputString = inputString.Substring(0, maxLength);
            }
            for (int i = 0; i < inputString.Length; i++)  //替换危险字符
            {
                switch (inputString[i])
                {
                    case '"':
                        retVal.Append("&quot;");
                        break;
                    case '<':
                        retVal.Append("&lt;");
                        break;
                    case '>':
                        retVal.Append("&gt;");
                        break;
                    default:
                        retVal.Append(inputString[i]);
                        break;
                }
            }
            retVal.Replace("'", "''");
        }
        return retVal.ToString();
    }

    public static string Clearstr2(string inputString)  //字符串过滤
    {
        StringBuilder retVal = new StringBuilder();
        if ((inputString != null) && (inputString != String.Empty))  //检查是否为空
        {
            inputString = inputString.Trim();
            retVal.Replace("'", "''");
        }
        return retVal.ToString();
    }

    /// <summary>
    /// 生成金币卡的卡号
    /// </summary>
    /// <returns></returns>
    public static string[] createGoldcardID(int iNum)
    {
        int num = iNum;
        Random ra = new Random();
        string[] str=new string[num];
        for (int i = 0; i < num; i++) {
            string id = "";
            string year = DateTime.Now.Year.ToString();
            string sDayOfYear = "";
            int iDayOfYear = DateTime.Now.DayOfYear;
            if (iDayOfYear < 10)
            {
                sDayOfYear = "00" + iDayOfYear.ToString();
            }
            else if (iDayOfYear >= 10 && iDayOfYear < 100)
            {
                sDayOfYear = "0" + iDayOfYear.ToString();
            }
            else
            {
                sDayOfYear = iDayOfYear.ToString();
            }
            string sHour = "";
            int iHour = DateTime.Now.Hour;
            if (iHour < 10)
            {
                sHour = "0" + iHour.ToString();
            }
            else
            {
                sHour = iHour.ToString();
            }

            string sMsecond = "";
            int iMsecond = DateTime.Now.Millisecond;
            if (iMsecond < 10)
            {
                sMsecond = "00" + iMsecond.ToString();
            }
            else if (iMsecond >= 10 && iMsecond < 100)
            {
                sMsecond = "0" + iMsecond.ToString();
            }
            else
            {
                sMsecond = iMsecond.ToString();
            }
            int aNum = ra.Next(1000, 9999);
            id = "gd" + year + sDayOfYear + sHour + sMsecond + aNum.ToString();
            str[i] = id;
        }

        return str;
    }

    /// <summary>
    /// 根据卡号生成金币卡的密码
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public static string createGoldcardPWD(string id)
    {
        string pwd = mymd5(id);
        pwd = pwd.ToLower().Substring(15, 16) + DateTime.Now.Year.ToString();
        pwd = mymd5(pwd);
        return pwd.Substring(15, 16);
    }
}